import type { RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
import { useRouteStore } from '../store';
import { getLocal } from '../utils';
export async function routerGuard(to:RouteLocationNormalized, next: NavigationGuardNext):Promise<boolean> {
  const route = useRouteStore();
  const isLogin = Boolean(getLocal('token'));
  // 初始化权限路由
  if (!route.isInitAuthRoute) {
    // 未登录情况下直接回到登录页，登录成功后再加载权限路由

    if (!isLogin) {
      if(to.name == 'login'){
        return true;
      }
      next({ name: 'login' });
      return false;
    }
    route.initRouter();
    if (to.name === 'not-found-page') {
      // 动态路由没有加载导致被not-found路由捕获，等待权限路由加载好了，回到之前的路由
      // 若路由是从根路由重定向过来的，重新回到根路由
      const ROOT_ROUTE_NAME = 'root';
      const path = to.redirectedFrom?.name === ROOT_ROUTE_NAME ? '/' : to.fullPath;
      next({ path, replace: true, query: to.query, hash: to.hash });
      return false;
    }
  }
  return true;
}
